package classes
{
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.DropShadowFilter;
    
    import mx.core.UIComponent;
    import mx.effects.Move;
    import mx.effects.Parallel;
    import mx.events.EffectEvent;


    public class PhotoStrip extends UIComponent
    {

		private var photo_arr:Array;
		//set the initial position at point 5 to give more left space
		private var x_pos:int = 20;
		private var y_pos:int = 0;

        public function PhotoStrip(){
        	photo_arr = new Array();
            super();
            
        }

		override protected function createChildren():void{
		    super.createChildren();
		}

		private var _data:Galleries = new Galleries();
		[Bindable] public function get data():Galleries {
			return _data;
		}
		public function set data(value:Galleries):void {
			_data = value;
		}
		
		private var _thumbwidth:int;
		public function get thumbwidth(): int {
			return _thumbwidth;
		}
		public function set thumbwidth(value:int): void {
			_thumbwidth = value;
		}
		
		private var _thumbheight:int;
		public function get thumbheight(): int {
			return _thumbheight;
		}
		public function set thumbheight(value:int): void {
			_thumbheight = value;
		}
		
		private var _padding:int;
		public function get padding(): int {
			return _padding;
		}
		public function set padding(value:int): void {
			_padding = value;
		}
		
		
		private var _effectduration:int;
		public function get effectduration(): int {
			return _effectduration;
		}
		public function set effectduration(value:int): void {
			_effectduration = value;
		}
		
		
		private var _thumbnailcount:int;
		public function get thumbnailcount(): int {
			return _thumbnailcount;
		}
		public function set thumbnailcount(value:int): void {
			_thumbnailcount = value;
		}
		
		private var _imagepath:String;
		public function get imagepath(): String {
			return _imagepath;
		}
		public function set imagepath(value:String): void {
			_imagepath = value;
		}
		
		
		
		public function init(): void {
			//every initialization, remove all childt
			
			//Alert.show(gallery.photos[0].source);
			
			remove_all_children();
			photo_arr = new Array();
			
			for (var i:int=0; i < data.galleries.length; i++)
			{
				//addChild(new image_class());
			    //var image:image_class = getChildAt(i) as image_class;
		        var image:image_class = new image_class();
		        image.addEventListener(MouseEvent.CLICK, listener_click(data.galleries[i].mainphoto, i));
		        image.addEventListener(MouseEvent.MOUSE_OVER, listener_hover(i));
		        image.addEventListener(MouseEvent.MOUSE_OUT, listener_out(i));
		        image.width = thumbwidth;
		        image.height = thumbheight;
		        image.x = x_pos+(i*(thumbwidth + padding));
		        image.y = y_pos;
		        image.type = 'RESIZE';
		        image.url = data.galleries[i].mainphoto;
		        image.visible = true;
		        image.buttonMode = true;
		        image.alpha = 0.5;
		        addChild(image);
		        
		        photo_arr.push(image);
		        //first picture should be selected during initialization
		        if(i==0){
		        	image.alpha = 1;
		        	toggleDropShadow(image);
		        }
			}
			create_mask();
			
		}
		
		public function navigate_left(): void {
			var parallel:Parallel = new Parallel();
			parallel.addEventListener(EffectEvent.EFFECT_END, listener_effect);
			data.is_enabled = false;
			
			for (var i:int=0; i < data.galleries.length; i++)
			{
			    var move:Move = new Move();
				move.target = photo_arr[i];
				move.duration = effectduration;
				move.xTo = photo_arr[i].x + (thumbwidth + padding);
				parallel.addChild(move);
				
			}
			
			parallel.play();
		}
		
		public function navigate_right(): void {
			var parallel:Parallel = new Parallel();
			parallel.addEventListener(EffectEvent.EFFECT_END, listener_effect);
			data.is_enabled = false;
			
			for (var i:int=0; i < data.galleries.length; i++)
			{ 
			    var move:Move = new Move();
				move.target = photo_arr[i];
				move.duration = effectduration;
				move.xTo = photo_arr[i].x - (thumbwidth + padding);
				parallel.addChild(move);
				
			}
			parallel.play();
		}
		
		public function listener_effect(evt:EffectEvent): void {
			data.is_enabled = true;
		}
		
		
		public function listener_click(flag:Object, index:int): Function {
			return function(): void {
				//decrease the alpha of previously selected image
				photo_arr[data.selected].alpha = 0.5;
				photo_arr[data.selected].filters = null;
				//photo_arr[gallery.selected].width = thumbsize;
				//photo_arr[gallery.selected].height = thumbsize;
				//increase the alpha of currently selected image
				photo_arr[index].alpha = 1;
				data.selected = index;
				toggleDropShadow(photo_arr[data.selected]);
				
				dispatchEvent(new Event('imageclick', true));
			}	
		}
		
		public function listener_hover(index:int): Function {
			return function(): void {
				if(index != data.selected){
					var parallel:Parallel = new Parallel();
					/*
					var resize:Resize = new Resize();
		            resize.target = photo_arr[index];
		            resize.duration = effectduration;
		            resize.widthTo = thumbsize + 5;
		            resize.heightTo = thumbsize + 5;
		            parallel.addChild(resize);
		            parallel.play();
		            */
		            photo_arr[index].alpha = 1;
		            toggleDropShadow(photo_arr[index]);
		  		}
	  		}
		}
		
		public function listener_out(index:int): Function {
			return function(): void {
				if(index != data.selected){
					/*
					var parallel:Parallel = new Parallel();
					var resize:Resize = new Resize();
		            resize.target = photo_arr[index];
		            resize.duration = effectduration;
		            resize.widthTo = thumbsize;
		            resize.heightTo = thumbsize;
		            parallel.addChild(resize);
		            parallel.play();
		            */
		            photo_arr[index].alpha = 0.5;
		            photo_arr[index].filters = null;
		  		}
	  		}
		}
		
		
		public function create_mask(): void {
			var largeMask:UIComponent = new UIComponent();
			largeMask.graphics.beginFill(0x00FFFF, 0.5);
			largeMask.graphics.drawRect(0, 0, (thumbnailcount*(thumbwidth+padding)), thumbwidth+5);
			largeMask.graphics.endFill();
			largeMask.x = x_pos;
			largeMask.y = 0;
			this.addChild(largeMask);
			this.mask = largeMask;
		}
		
		
		public function remove_all_children(): void {
			while (this.numChildren > 0) {     
				this.removeChildAt(0); 
			}
		}
		
		private function toggleDropShadow(image:Object):void {  
             var ds:DropShadowFilter = new DropShadowFilter();  
             ds.distance = 1;  
             ds.alpha = .6;  
             image.filters = [ds];  
              
        }


		
    }
}